<?php
if(!defined("YUEXUAN_REAL_ROOT_PATH")){
	exit("Access Denied.");
}

/**
 * kvdb的接口
 * 在腾讯云平台上使用ceme，在自己的服务器上使用redis
 * 由于ceme仅支持kvdbd的接口，所以在自己服务器上
 * 使用redis时也仅开放和kvdbd类似的几个接口
 */ 
class ext_kvdb {
	private $_kvdb;
	private static $_instance;
    private $_keyCacheTime = array(
    	"session" => 1800,
    	"wcuser" => 0,
        "usergameinfo" => 0,
        "newplayer" => 0,
        "sig" => 0,
    	"yellow" => 0,
        "friend" => 0,
        "invite" => 0,
        "freegift" => 0,
    	"egggift" => 0,
    	"onlinestarttime" => 3600,
    	"userTopScore" => 0,
    	"userTodayTopScore" => 0,
    	"placeSocreSectionCount" => 0,
    	"gameAuthID" => 0,
    	"userFreeLifeCount" => 0,
    	"shareToken" => 1800,
    );
	
	private function __construct() {
		// 根据配置信息决定连接ceme还是redis
		if(KVDB_TYPE=="ceme"){
			$this->_kvdb = new ext_ceme();
		}else{
			$this->_kvdb = new ext_redis();
		}
	}
	private function __clone() {
		 
	}
	/**
	 * 单例模式下取得kvdb接口
	 * @return array
	 */
	public static function getInstance() {
		if(!(self::$_instance instanceof self)) {
			self::$_instance = new self();
		}
		return self::$_instance;
	}

	/**
	 * 添加一个key
	 * @param string $identify
	 * @param array $condition
	 * @param mixd $value
	 */
	public function add($identify, $condition=array(), $value, $cacheTime=null){
		$key = $this->createKey($identify, $condition);
		if($cacheTime===null){
			$cacheTime = $this->_keyCacheTime[$identify];
		}
		$logger = ext_logger::getInstance();
		$logger->debug("add添加一个kvdb key： ".$key);
		return $this->_kvdb->add($key, $value, $cacheTime);
	}
	/**
	 * 取得数据
	 * @param string $identify
	 * @param array $condition
	 * @return mixed
	 */
	public function get($identify, $condition=array()){
		$key = $this->createKey($identify, $condition);
		$logger = ext_logger::getInstance();
		$logger->debug("get读取kvdb数据： ".$key);
		return $this->_kvdb->get($key);
	}
	/**
	 * 保存数据
	 * @param string $identify
	 * @param array $condition
	 * @param mixed $value
	 */
	public function set($identify, $condition, $value, $cacheTime=null){
		$key = $this->createKey($identify, $condition);
		if($cacheTime===null){
			$cacheTime = $this->_keyCacheTime[$identify];
		}
		$logger = ext_logger::getInstance();
		$logger->debug("set设置kvdb数据： ".$key.print_r($value,1));
		$this->_kvdb->set($key, $value, $cacheTime);
	}
	/**
	 * 删除数据
	 * @param string $identify
	 * @param array $condition
	 */
	public function delete($identify, $condition=array()){
		$key = $this->createKey($identify, $condition);
		$logger = ext_logger::getInstance();
		$logger->debug("删除kvdb数据： ".$key);
		$this->_kvdb->delete($key);
	}
	
	/**
	 * 增加
	 * @param string $identify
	 * @param array $condition
	 * @param int $value
	 */
	public function increment($identify, $condition=array(), $value=1){
		$key = $this->createKey($identify, $condition);
		$logger = ext_logger::getInstance();
		$logger->debug("增加kvdb数据的值： ".$key);
		$this->_kvdb->increment($key, $value);
	}
	
	/**
	 * 减少
	 * @param string $identify
	 * @param array $condition
	 * @param int $value
	 */
	public function decrement($identify, $condition=array(), $value=1){
		$key = $this->createKey($identify, $condition);
		$logger = ext_logger::getInstance();
		$logger->debug("减少kvdb数据的值： ".$key);
		$this->_kvdb->decrement($key, $value);
	}
	
	/**
	 * 根据条件创建key
	 * @param string $identify
	 * @param array $condition
	 * @return string
	 */
	private function createKey($identify, $condition=array()){
		if(isset($this->_keyCacheTime[$identify])){
			$key = $identify;
			foreach($condition as $k=>$v){
				$key .= "_".$k."_".$v;
			}
			return KVDB_TABLE_PRE.$key;
		}else{
			$logger = ext_logger::getInstance();
			$logger->error($identify."不是有效的kvdb key");
			throw new Exception($identify."不是有效的kvdb key");
		}
		 
	}
}

?>